package com.lxhll.personnel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxhll.personnel.core.P;
import com.lxhll.personnel.core.ResponseData;
import com.lxhll.personnel.entity.Position;
import com.lxhll.personnel.service.PositionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * (Position)表控制层
 *
 * @author makejava
 * @since 2021-07-18 01:06:38
 */
@RestController
@RequestMapping("/position")
@Slf4j
public class PositionController {
    /**
     * 服务对象
     */
    @Resource
    private PositionService positionService;

    /**
     * 查询岗位所有下员工，分页
     */
    @PostMapping("/list")
    public ResponseData list(@RequestBody Map<String ,Object> params){
        //提取查询参数与分页
        log.info("查询参数:"+params);
        //构建wrapper
        QueryWrapper<Position> wrapper= new QueryWrapper<>();
        //查询岗位列表,可以按照岗位编号，名称，类型查询
        Integer id=(Integer)params.get("id");
        String name=(String)params.get("name");
        String type=(String)params.get("type");
        if(id!=null)
            wrapper.like("id",id);
        if(StringUtils.isNotBlank(name))
            wrapper.like("name",name);
        if(StringUtils.isNotBlank(type))
            wrapper.like("type",type);
        IPage<Position> positionIPage=positionService.page(P.page(params),wrapper);
        System.out.println(positionIPage.getRecords());
        //返回
        return ResponseData.ok(positionIPage);
    }

    /**
     * 获取岗位列表，作为职工操作时前台下拉框
     * @return
     */
    @GetMapping("/droplist")
    public ResponseData droplist(){
        return ResponseData.ok(positionService.list());
    }



    /**
     * 查询单个岗位信息
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public ResponseData info(@PathVariable("id") Integer id){
        Position Position = positionService.getPositionById(id);
        return ResponseData.ok(Position);
    }

    /**
     * 删除岗位，可以批量.如果该岗位下有员工，则不能删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    public ResponseData deletePosition(@RequestBody Integer[] ids){
        log.info("删除岗位id"+ Arrays.toString(ids));
        boolean res=positionService.removePositionByIds(ids);
        if(!res){
            return ResponseData.error("岗位下有所属员工，不能删除！");
        }
        return ResponseData.ok();
    }

    /**
     * 岗位信息修改
     * @paramPosition
     * @return
     */
    @PutMapping(value="/update")
    public ResponseData update(@Validated @RequestBody Position position) {
        positionService.updatePositionById(position);
        return ResponseData.ok();
    }

    /**
     * 增加岗位
     */
    @PostMapping("/save")
    public ResponseData add(@Validated @RequestBody Position position){
        boolean res=positionService.save(position);
        return ResponseData.ok();
    }

}
